package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.service.other.domain.Assets;
import com.qiaofang.applet.service.other.domain.AssetsType;
import com.qiaofang.applet.service.other.service.AssetsService;
import com.qiaofang.applet.service.other.service.dto.*;
import com.qiaofang.applet.service.other.service.mapstruct.AssetsMapper;
import com.qiaofang.applet.service.property.service.dto.IdDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author daiyuxaing
 */
@Slf4j
@RestController
@RequestMapping("/api/web/assets")
@Api(tags = "资讯：资讯服务")
public class AssetsController {

    @Autowired
    private AssetsService assetsService;

    @Autowired
    private AssetsMapper assetsMapper;

    @PostMapping("/searchAssetsType")
    @ApiOperation(value = "查询资讯服务类型", notes = "查询资讯服务类型")
    public DataResponse<List<AssetsTypeDto>> searchAssetsType() {
        List<AssetsTypeDto> allType = assetsService.findAllType(QfContextHolder.getContext().getCorporationCode());
        AssetsTypeDto assetsTypeDto = new AssetsTypeDto();
        assetsTypeDto.setCorporationCode(QfContextHolder.getContext().getCorporationCode());
        assetsTypeDto.setAssetsTypeName("全部");
        allType.add(0,assetsTypeDto);
        return DataResponse.ok(allType);
    }


    @PostMapping("/searchAssets")
    @ApiOperation(value = "查询资讯服务列表", notes = "查询资讯服务列表")
    public DataResponse<Page<AssetsDetailDto>> searchAssets(@RequestBody PageDataRequest<AssetsQueryDto> request) {
        Page<AssetsDetailDto> result = assetsService.getListForClient(request.getParam(), PageUtil.getPageable(request));
        return DataResponse.ok(result);
    }


    @PostMapping("/getAssets")
    @ApiOperation(value = "查询资讯服务详情", notes = "查询资讯服务详情")
    public DataResponse<AssetsDetailDto> getAssets(@RequestBody DataRequest<IdDto> request) {
        Optional<Assets> assets = assetsService.findById(request.getParam().getId());
        AssetsDetailDto result = assets.map(assetsMapper::toDto).orElseThrow(() -> new BusinessException("资讯服务业务异常"));

        if(result != null && StringUtils.isNotEmpty(result.getAssetsTypeId())) {
            AssetsType assetsType = assetsService.findAssetsType(assets.get().getAssetsTypeId());
            result.setAssetsTypeName(assetsType.getAssetsTypeName());
        }
        return DataResponse.ok(result);
    }

    @PostMapping("/getIndexAssets")
    @ApiOperation(value = "首页咨询内容", notes = "首页咨询内容")
    public DataResponse<List<List<AssetsDetailDto>>> getIndexAssets(@RequestBody PageDataRequest<AssetsQueryDto> request) {
        return DataResponse.ok(assetsService.getIndexAssets(request.getParam().getCityCode(), PageUtil.getPageable(request)));
    }

    @PostMapping("/shareAssets")
    @ApiOperation(value = "咨询分享朋友圈二维码", notes = "咨询分享朋友圈")
    public DataResponse<ShareAssetsDto> shareAssets(@RequestBody DataRequest<IdDto> request) {
        return DataResponse.ok(assetsService.shareAssets(request.getParam().getId()));
    }

    @PostMapping("/getAssetsPhotoLists")
    @ApiOperation(value = "获取咨询照片合集", notes = "获取咨询照片合集")
    public DataResponse<List<String>> getAssetsPhotoLists(@RequestBody DataRequest<IdDto> request) {
        return DataResponse.ok(assetsService.getAssetsPhotoLists(request.getParam().getId()));
    }
}
